package es.udc.emarketapp.http.controller.actions.clients;

import es.udc.emarketapp.emarket.model.client.vo.ClientVO;
import es.udc.emarketapp.emarket.model.country.vo.CountryVO;
import es.udc.emarketapp.emarket.model.countryFacade.CountryFacade;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import es.udc.emarketapp.emarket.model.usersFacade.UsersFacade;
import es.udc.emarketapp.http.controller.session.SessionManager;
import es.udc.emarketapp.http.view.actionForms.ClientForm;
import es.udc.emarketapp.http.view.applicationObjects.DateRanges;
import es.udc.fbellas.j2ee.util.exceptions.InstanceNotFoundException;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.struts.action.DefaultAction;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

public class EditClientDetailsAction extends DefaultAction {

    public ActionForward doExecute(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response)
            throws IOException, ServletException, InternalErrorException {

        ClientForm clientForm = (ClientForm) form;

        ActionMessages errors = new ActionMessages();

        ClientVO clientVO;

        String idClient = SessionManager.getIdUser(request);

        UsersFacade usersFacade = new UsersFacade();


        try {
            clientVO = usersFacade.findClientById(idClient);

            clientForm.setIdClient(idClient);
            clientForm.setName(clientVO.getNomPila());
            clientForm.setSurname1(clientVO.getAp1());
            clientForm.setSurname2(clientVO.getAp2());
            clientForm.setNifCif(clientVO.getNifCif());
            clientForm.setEmail(clientVO.getEmail());
            clientForm.setTlf1(clientVO.getTlf1());
            clientForm.setTlf2(clientVO.getTlf2());
            Calendar fnac = clientVO.getFNac();
            clientForm.setBirthDay(String.valueOf(fnac.DAY_OF_YEAR));
            clientForm.setBirthMonth(String.valueOf(fnac.MONTH));
            clientForm.setBirthYear(String.valueOf(fnac.YEAR));
            clientForm.setAddress(clientVO.getDir());
            clientForm.setCity(clientVO.getCiudad());
            clientForm.setCp(clientVO.getCp());
            clientForm.setProvince(clientVO.getProv());
            clientForm.setCountry(clientVO.getPais());
            clientForm.setAddressSend(clientVO.getDirEnvio());
            clientForm.setCitySend(clientVO.getCiudadEnvio());
            clientForm.setCpSend(clientVO.getCpEnvio());
            clientForm.setProvinceSend(clientVO.getProvEnvio());
            clientForm.setCountrySend(clientVO.getPaisEnvio());

          //  request.setAttribute("dateRanges", new DateRanges());
            CountryFacade countryFacade = new CountryFacade();

            Locale locale = getLocale(request);
            List<CountryVO> countryVOs = countryFacade.findAllCountries(locale.getLanguage());
            request.setAttribute("countries", countryVOs);


        } catch (InstanceNotFoundException e) {

                errors.add("identifier",
                    new ActionMessage("ErrorMessages.client.notFound"));
            saveErrors(request, errors);

            return new ActionForward(mapping.getInput());

        }

        return mapping.findForward("UpdateClientDetailsForm");

    }
}
